#include "stdafx.h"

// *****************************************************************************************
//
// NAPYR2 (http://p-nand-q.com/python/napyr.html)
//
// Copyright (c) 2014, Gerson Kurz
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright notice, this
//    list of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its contributors may
//    be used to endorse or promote products derived from this software without
//    specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// *****************************************************************************************

#include "python.h"
#include "napyr2.h"
#include "napyr2_Object.h"

namespace napyr2 { // begins here

PythonObject PythonObject::None(Py_None);
PythonObject PythonObject::True(Py_True);
PythonObject PythonObject::False(Py_False);

PythonObject::PythonObject( PyObject* value )
	:	m_pyObject(value)
{
	if( m_pyObject )
	{
		Py_INCREF(m_pyObject);
	}
}

PythonObject::PythonObject( const PythonObject& objectSrc )
	:	m_pyObject( objectSrc.m_pyObject )
{
	if( m_pyObject )
	{
		Py_INCREF(m_pyObject);
	}
}

PythonObject& PythonObject::operator=( const PythonObject& objectSrc )
{
	if( this != &objectSrc )
	{
		if( m_pyObject )
		{
			Py_DECREF(m_pyObject);
		}
		m_pyObject = objectSrc.m_pyObject;
		if( m_pyObject )
		{
			Py_INCREF(m_pyObject);
		}
	}
	return *this;
}

PythonObject::PythonObject( LPCSTR value )
	:	m_pyObject(PyString_FromString(value))
{

}

PythonObject::PythonObject( LPCWSTR value )
	:	m_pyObject(PyUnicode_FromWideChar(value, wcslen(value)))
{
}

PythonObject::PythonObject( const std::string& value )
	:	m_pyObject(PyString_FromString(value.c_str()))
{
}

PythonObject::PythonObject( long value )
	:	m_pyObject(PyInt_FromLong(value))
{
}

PythonObject::PythonObject( unsigned long value )
	:	m_pyObject(PyInt_FromLong(value))
{
}

PythonObject::PythonObject( int value )
	:	m_pyObject(PyInt_FromLong(value))
{
}

PythonObject::PythonObject( unsigned int value )
	:	m_pyObject(PyInt_FromLong(value))
{
}

PyObject* PythonObject::AddRef() const
{
	if( m_pyObject )
	{
		Py_INCREF(m_pyObject);
	}
	return m_pyObject;
}

PythonObject::PythonObject( long long value )
	:	m_pyObject(PyLong_FromLongLong(value))
{
}

PythonObject::PythonObject( unsigned long long value )
	:	m_pyObject(PyLong_FromLongLong(value))
{
}

PythonObject::PythonObject( double value )
	:	m_pyObject(PyFloat_FromDouble(value))
{
}

PythonObject::~PythonObject()
{
	if( m_pyObject )
	{
		Py_DECREF(m_pyObject);
		m_pyObject = NULL;
	}
}

std::string g_strDefaultEncoding("latin-1");
std::string g_strDecodeOptions("strict");

bool PythonObject::GetFromObject( PyObject* pyObject, std::string& value ) 
{
	if( !pyObject )
		return false;

	if( PyString_Check(pyObject) )
    {
        value = PyString_AsString(pyObject);
		return true;
    }
	
	if( PyUnicode_Check(pyObject) )
	{
        PyObject* encoded = PyUnicode_AsEncodedString(pyObject, g_strDefaultEncoding.c_str(), g_strDecodeOptions.c_str());
        if( !encoded )
			return false;

        value = PyString_AsString(encoded);
        Py_DECREF(encoded);
        return true;
    }
	
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, int& value ) 
{
	if( !pyObject )
		return false;

	if( PyLong_Check(pyObject) )
	{
        value = (int) (long) PyLong_AsLongLong(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = (int) PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = atoi(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, unsigned int& value ) 
{
	if( !pyObject )
		return false;

	if( PyLong_Check(pyObject) )
	{
        value = (int) (long) PyLong_AsLongLong(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = (int) PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = atoi(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, long& value ) 
{
	if( !pyObject )
		return false;

	if( PyLong_Check(pyObject) )
	{
        value = (long) PyLong_AsLongLong(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = atoi(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, unsigned long& value ) 
{
	if( !pyObject )
		return false;

	if( PyLong_Check(pyObject) )
	{
        value = (long) PyLong_AsLongLong(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = atoi(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, long long& value ) 
{
	if( !pyObject )
		return false;

	if( PyLong_Check(pyObject) )
	{
        value = PyLong_AsLongLong(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = _atoi64(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, unsigned long long& value ) 
{
	if( !pyObject )
		return false;

	if( PyLong_Check(pyObject) )
	{
        value = PyLong_AsLongLong(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = _atoi64(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, double& value ) 
{
	if( !pyObject )
		return false;

	if( PyFloat_Check(pyObject) )
	{
        value = PyFloat_AS_DOUBLE(pyObject);
		return true;
	}

    if( PyInt_Check(pyObject) )
	{
        value = PyInt_AS_LONG(pyObject);
		return true;
	}

    std::string s;
	if( GetFromObject(pyObject, s) )
	{
		value = atof(s.c_str());
		return true;
	}
	return false;
}

bool PythonObject::GetFromObject( PyObject* pyObject, bool& value ) 
{
	if( !pyObject )
		return false;

	value = PyObject_IsTrue(pyObject) ? true : false;
	return true;
}

} // namespace napyr2 ends here.


